package leetcode.f1t100;

/**
 * 数独校验
 * https://leetcode.com/problems/valid-sudoku/
 *
 * @Author lichongxiao
 * @Date 2022/10/12 14:07
 */
public class Q36_ValidSudoku {
    public boolean isValidSudoku(char[][] board) {
        /**
         * flag[0][m][n]表示第m行有n值;
         * flag[1][m][n]表示第m列有n值;
         * flag[2][m][n]表示第m个九宫格有n值;
         */
        boolean[][][] flag = new boolean[3][9][10];
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if ('.' != board[i][j]) {
                    if(!setFlag(flag, i, j, board[i][j])){
                        return false;
                    };
                }
            }
        }
        return true;
    }

    public static boolean setFlag(boolean[][][] flag, int i, int j, char value) {
        int intVal = value - '0';
        if(flag[0][i][intVal] || flag[1][j][intVal] || flag[2][3 * (i / 3) + (j / 3)][intVal]){
            return false;
        }
        flag[0][i][intVal] = true;
        flag[1][j][intVal] = true;
        flag[2][3 * (i / 3) + (j / 3)][intVal] = true;
        return true;
    }

}

